<?php
declare(strict_types=1);

namespace LowCode;

use LowCode\Abstracts\Core;
use LowCode\Traits\HasCall;

/**
 * 依赖包库
 * @method static $this make(string $package, string $version, string $library, array $urls) 创建依赖
 * @method $this package(string $package) 包名
 * @method $this version(string $version) 版本号
 * @method $this library(string $library) 库导出名称
 * @method $this required(bool $bool) 是否必须依赖
 * @method $this official(bool $bool) 是否官方内置提供
 * @method $this enabled(bool $bool) 是否启用
 * @method $this localeLibrary(string $localeLibrary) 语言包库导出名称
 * @method $this urls(array $urls) 加载资源url数组
 * @method $this assetsUrl(string $assetsUrl) 资产配置url
 * @method $this assetsLibrary(string $assetsLibrary) 资产配置导出名称
 */
class Dependencies extends Core
{
    use HasCall;

    protected static array $allowAttributes = [
        "package","version","library","required","official","enabled","localeLibrary","urls","assetsUrl","assetsLibrary"
    ];

    /**
     * @param string $package 包名
     * @param string $version 版本号
     * @param string $library 库导出名称
     * @param array $urls 加载资源url
     */
    public function __construct(string $package, string $version, string $library, array $urls)
    {
        parent::__construct([
            "package"=>$package,
            "version"=>$version,
            "library"=>$library,
            "urls"=>$urls,
        ]);
    }

    /**
     * 新增url
     * @param string $url
     * @return $this
     */
    public function addUrl(string $url): static
    {
        $urls = $this->get("urls",[]);
        $urls[] = $url;
        return $this->urls($urls);
    }

    /**
     * 删除url
     * @param string $url
     * @return $this
     */
    public function removeUrl(string $url): static
    {
        $urls = $this->get("urls",[]);
        $search = array_search($url, $urls);
        if ($search!==false){
            unset($urls[$search]);
        }
        return $this->urls($urls);
    }

    public function __call($method, $parameters)
    {
        return $this->toCall($method, $parameters);
    }

}
